<?php
/**
 * 所有lumesse系统部门
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class("Bll_Flow_Lumesse");
class Oa_Job_Flow_SyncLumesseDepartment extends Oa_Job_Abstract {
    private $write_db = "hroa_master";//写数据库
    public function run() {
        $this->log("开始同步Lumesse系统部门信息".date('Y-m-d H:i:s'));
        $res = $this->_get_depart_lum();

        $res = $this->_build_data_depart($res);
        if(isset($res['add']) && !empty($res['add'])){
            foreach($res['add'] as $add){
                $this->_add_departs($add);
            }
        }
        if(isset($res['edit']) && !empty($res['edit'])){
            foreach($res['edit'] as $edit){
                $this->_update_departs($edit);
            }
        }
        if(isset($res['delete']) && !empty($res['delete'])){
            foreach($res['delete'] as $delete){
                $this->_delete_departs($delete['lums_depart_id']);
            }
        }
        $this->log("同步Lumesse系统部门信息处理完".date('Y-m-d H:i:s'));
    }
    private function _build_data_depart($new){
        $result = array();
        $old = $this->_get_departs();
        $old = $this->_build_key_value('lums_depart_id',$old);
        $new = $this->_build_key_value('lums_depart_id',$new);
        $old_keys = array_keys($old);
        $new_keys = array_keys($new);
        foreach($old as $key=>$one){
            if(in_array($key, $new_keys)){
                if(!$this->_comp($one, $new[$key])){
                    $result['edit'][] = $new[$key];
                }
            }else{
                $result['delete'][] = $one;
            }
        }
        foreach($new as $key=>$one){
            if(!in_array($key, $old_keys)){
                $result['add'][] = $one;
            }
        }
        return $result;
    }
    private function _comp($old,$new){
        $res = true;
        foreach($new as $key=>$value){
            if($old[$key] != $value){
                $res = false;
            }
        }
        return $res;
    }
    private function _build_key_value($key,$data){
        $result = array();
        foreach($data as $one){
            $result[$one[$key]] = $one;
        }
        return $result;
    }
    private function _get_depart_lum(){
        $lumesse_user_organization = Bll_Flow_Lumesse::get_instance()->Lumesse_User()->Organization();
        $organizations = $lumesse_user_organization->getOrganizations();
        $organizations = $organizations->organization->organization;
        return $this->_build_data($organizations);
    }
    /**
     * 处理部门数据
     */
    private function _build_data($organizations){
        $result = array();
        $tmp_depart_id = array();
        $key = 0;
        foreach($organizations as $organization){
            $parent_id = 0;
            foreach($organization->unit as $or){
                if($or->id && !in_array($or->id, $tmp_depart_id)){
                    $result[$key]['lums_depart_id'] = $or->id;
                    $result[$key]['lums_depart_name'] = $or->_;
                    $result[$key]['lums_depart_ename'] = $or->code;
                    $result[$key]['lums_depart_level'] = $or->level;
                    $result[$key]['lums_pdepart_id'] = $parent_id;
                    $key++;
                    $tmp_depart_id[] = $or->id;
                }
                $parent_id = $or->id;
            }
        }
        return $result;
    }
    private function _add_departs($data){
        $sql = "INSERT INTO lumesse_depart(lums_depart_id,lums_depart_ename,lums_depart_name,lums_pdepart_id,lums_depart_level) VALUES(";
        $sql.="".$data['lums_depart_id'].',';
        $sql.="'".str_replace("'", "''", $data['lums_depart_ename'])."',";
        $sql.="'".str_replace("'", "''", $data['lums_depart_name'])."',";
        $sql.="".$data['lums_pdepart_id'].",";
        $sql.="".$data['lums_depart_level']."";
        $sql.=")";
        $this->execute( $sql,array(), $this->write_db);
    }
    private function _get_departs(){
        $sql = "SELECT * from lumesse_depart ";
        return $this->get_db_rows($sql,$this->write_db,array());
    }
    private function _delete_departs($lums_depart_id){
        $sql.="DELETE FROM lumesse_depart WHERE lums_depart_id=".$lums_depart_id;
        $this->execute( $sql,array(), $this->write_db);
    }
    private function _update_departs($data){
        $sql = "UPDATE lumesse_depart SET ";
        $sql.="lums_depart_ename='".str_replace("'", "''", $data['lums_depart_ename'])."',";
        $sql.="lums_depart_name='".str_replace("'", "''", $data['lums_depart_name'])."',";
        $sql.="lums_pdepart_id=".$data['lums_pdepart_id'].",";
        $sql.="lums_depart_level=".$data['lums_depart_level']."";
        $sql.=" WHERE lums_depart_id=".$data['lums_depart_id'];
        $this->execute( $sql,array(), $this->write_db);
    }
}